//
//  MNDChangePhoneSendCaptchaViewController.m
//  minidai
//
//  Created by Steven.Yung on 2017/5/8.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDChangePhoneSendCaptchaViewController.h"
#import "MNDChangePhoneViewController.h"
#import "NSString+ReplaceAsterisk.h"
#import "MNDChangePhoneViewController.h"
#import "UIImage+JKColor.h"
#import "UIButton+JKCountDown.h"

@interface MNDChangePhoneSendCaptchaViewController ()
@property (weak, nonatomic) IBOutlet UILabel *noticeLabel;
@property (weak, nonatomic) IBOutlet UITextField *captchaTextField;
@property (weak, nonatomic) IBOutlet UIButton *captchaBtn;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;

@property (copy, nonatomic) NSString *phoneCaptcha;

@end

@implementation MNDChangePhoneSendCaptchaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.noticeLabel.text = [NSString stringWithFormat:@"您现在使用的手机号码是%@，更换号码后，下次登录需要使用新手机号", [self.userModel.userPhone replaceWithAsteriskAtRange:NSMakeRange(3, 4)]];
    
    [self.submitBtn setBackgroundImage:[UIImage imageNamed:@"invest_result_continue_btn"] forState:UIControlStateNormal];
    [self.submitBtn setBackgroundImage:[UIImage jk_imageWithColor:kColorLightGray] forState:UIControlStateDisabled];
    
    RAC(self, phoneCaptcha) = self.captchaTextField.rac_textSignal;
    RAC(self.submitBtn, enabled) = [RACSignal combineLatest:@[RACObserve(self, phoneCaptcha)] reduce:^id (NSString *captcha){
        
        return @(captcha.length > 0);
    }];
    
    __block UITextField *captchaTextField = self.captchaTextField;
    [self.captchaTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 6) {
            captchaTextField.text = [x substringToIndex:6];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)sendCaptchaAction:(id)sender {
    
    [NetworkProgressHUD showHud];
    
    [APIManager sendRequestWithPath:API_SEND_OLD_PHONE_CAPTCHA parameters:[MNDSign signedParam] method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD showHudMsg:@"验证码已发送"];
            UIButton *btn = sender;
            [btn jk_startTime:30 title:@"重新发送" waitTittle:@"秒后重发"];
        } else {
            NSDictionary *dict = responseModel.msg;
            if (dict && dict.count > 0) {
                [NetworkProgressHUD showHudMsg:dict[@"error"]];
                
            } else {
                [NetworkProgressHUD showHudMsg:@"获取数据失败"];
            }
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

- (IBAction)toNextAction:(id)sender {
    
    WeakSelf(self);
    [NetworkProgressHUD showHud];
    NSDictionary *params = [MNDSign signedParamWithDict:@{@"phone_captcha": self.phoneCaptcha}];
    
    [APIManager sendRequestWithPath:API_PHONE_VERIFY parameters:params method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD hideHud];
            [weakself performSegueWithIdentifier:[MNDChangePhoneViewController description] sender:nil];
        } else {
            NSDictionary *dict = responseModel.msg;
            if (dict && dict.count > 0) {
                [NetworkProgressHUD showHudMsg:dict[@"error"]];
                
            } else {
                [NetworkProgressHUD showHudMsg:@"获取数据失败"];
            }
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

//    if ([segue.identifier isEqualToString:[MNDChangePhoneViewController description]]) {
//        MNDChangePhoneViewController *changeVC = segue.destinationViewController;
////        changeVC.
//    }
}


@end
